﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfDrawingView : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
            }
            strPubId = this.GetFormParam("ID");
            this.ExecJScript("$('#txxtid').val('" + strPubId + "');");
        }

        private string strPubId = "";

        private void BindControls()
        {
            if (!IsPostBack)
            { 
            }
        }

        public string GetDataListJavaScriptPub(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(GetDataSet().Tables[0], "", ""
                , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                , (strSort == string.Empty ? "" : strSort));

            return " <script type=\"text/javascript\">var DataPub=" + strjsontem1 + "</script>";
        }

        private DataSet GetDataSet()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpDrawPub opub = new AppLogic.DataAlc.CBizShpDrawPub())
            {
                DataSet ds = opub.DataObj.QueryBySQL(@" 
    SELECT  
    A.DRAW_PUB_ID
    ,DRAW_TITLE=CASE WHEN ISNULL(A.DRAW_TITLE,'')='' THEN V1.PRIZE_NME ELSE A.DRAW_TITLE END 
    ,PARTAKE_DT_S=STUFF(STUFF(CONVERT(char(8),A.PARTAKE_DT_S,112),5,0,N'年'),8,0,N'月')+N'日 ' + CONVERT(varchar(8), A.PARTAKE_DT_S, 24)
    ,PARTAKE_DT_E=STUFF(STUFF(CONVERT(char(8),A.PARTAKE_DT_E,112),5,0,N'年'),8,0,N'月')+N'日 ' + CONVERT(varchar(8), A.PARTAKE_DT_E, 24)
    ,DRAWING_DT=STUFF(STUFF(CONVERT(char(8),A.DRAWING_DT,112),5,0,N'年'),8,0,N'月')+N'日 ' + CONVERT(varchar(8), A.PARTAKE_DT_E, 24)
    ,DRAWING_EXPIRE_DT=STUFF(STUFF(CONVERT(char(8),A.DRAWING_EXPIRE_DT,112),5,0,N'年'),8,0,N'月')+N'日'  
	,DRAWING_STATUS=CASE WHEN  A.PARTAKE_DT_S>GETDATE()  THEN '未开启' ELSE 
						(CASE WHEN  A.PARTAKE_DT_E>GETDATE()  THEN '正在抽奖' ELSE 
							(CASE WHEN  A.DRAWING_DT>GETDATE() THEN '等待开奖' ELSE 
								(CASE WHEN DATEDIFF(DAY,A.DRAWING_EXPIRE_DT,GETDATE())<0 THEN '兑奖中' ELSE 
									'已关闭'
								END) 
							END) 
						END)
					END
    ,DRAW_TITLE_IMG_URL=CASE WHEN ISNULL(A.DRAW_TITLE_IMG_URL,'')='' THEN V1.PRIZE_IMG_URL ELSE A.DRAW_TITLE_IMG_URL END
	,WINNER_COUNT=ISNULL(V1.WINNER_COUNT,0)
	,PARTAKE_COUNT=ISNULL(V2.PARTAKE_COUNT,0)
    ,V3.WIN_NO
    ,V3.PRIZE_CLASS
    ,V3.PRIZE_NME
    ,USER_NME='" + msBsc.SvRef.UserInfo.Operator + @"'
    FROM SHP_DRAW_PUB A 
	LEFT OUTER JOIN 
	(
	SELECT DRAW_PUB_ID,WINNER_COUNT=SUM(WINNER_COUNT),PRIZE_NME=MAX(PRIZE_NME),PRIZE_IMG_URL=MAX(PRIZE_IMG_URL) FROM SHP_DRAW_PRIZE WHERE DRAW_PUB_ID='" + strPubId + @"' GROUP BY DRAW_PUB_ID
	) AS V1 ON A.DRAW_PUB_ID=V1.DRAW_PUB_ID 
	LEFT OUTER JOIN 
	(
	SELECT DRAW_PUB_ID,PARTAKE_COUNT=COUNT(*) FROM SHP_DRAW_PARTAKE WHERE DRAW_PUB_ID='" + strPubId + @"' GROUP BY DRAW_PUB_ID
	) AS V2 ON A.DRAW_PUB_ID=V2.DRAW_PUB_ID
    LEFT OUTER JOIN
    (
    SELECT A.DRAW_PUB_ID,A.WIN_NO,C.PRIZE_CLASS,C.PRIZE_NME FROM SHP_DRAW_PARTAKE A LEFT OUTER JOIN  SHP_DRAW_WINNER B ON A.DRAW_PUB_ID=B.DRAW_PUB_ID AND A.WIN_NO=B.WIN_NO LEFT OUTER JOIN SHP_DRAW_PRIZE C ON B.DRAW_PRIZE_ID=C.DRAW_PRIZE_ID WHERE A.DRAW_PUB_ID='" + strPubId + @"' AND A.USER_NME='" + msBsc.SvRef.UserInfo.Operator + @"'
    ) AS V3 ON A.DRAW_PUB_ID=V3.DRAW_PUB_ID
    WHERE A.DRAW_PUB_ID='" + strPubId + @"' 
");
                return ds;
            }
        }

        public string GetDataListJavaScriptPrize(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpDrawPrize oCBizShpDrawPrize = new AppLogic.DataAlc.CBizShpDrawPrize())
            {
                DataSet ds = oCBizShpDrawPrize.DataObj.QueryBySQL(@" 
    SELECT 
    A.SEQ_NUM
    ,A.PRIZE_CLASS
    ,A.PRIZE_NME
    ,A.PRIZE_DESC
    ,A.PRIZE_GOODS_URL
    ,A.PRIZE_SHOP_NME
    ,A.PRIZE_SHOP_URL
    ,A.PRIZE_IMG_URL
    ,A.WINNER_COUNT
    ,B.USER_NME  
    FROM SHP_DRAW_PRIZE A 
    LEFT OUTER JOIN SHP_DRAW_WINNER C ON A.DRAW_PRIZE_ID=C.DRAW_PRIZE_ID
    LEFT OUTER JOIN SHP_DRAW_PARTAKE B ON A.DRAW_PUB_ID=B.DRAW_PUB_ID AND C.WIN_NO=B.WIN_NO 
    WHERE A.DRAW_PUB_ID='" + strPubId + @"'
");
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "SEQ_NUM,PRIZE_CLASS,PRIZE_NME,PRIZE_DESC,PRIZE_GOODS_URL,PRIZE_SHOP_NME,PRIZE_SHOP_URL,PRIZE_IMG_URL,WINNER_COUNT"
                    , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "SEQ_NUM ASC " : strSort));

                return " <script type=\"text/javascript\">var DataPrize=" + strjsontem1 + "</script>";
            }
        }
         

        public override void DoAction()
        {
            if (this.strAction == "REGIST_STEP1")
            {
                if (this.GetParamHtValue("VALIDATE_CDE")==string.Empty || this.GetParamHtValue("VALIDATE_CDE") != (string)GetSession(this.sCurrPageRadom + msBsc.Cls.ValidateCode.VALIDATECODEKEY, "2"))
                {
                    this.ExecJScript("jQuery.getValidateImg('J_CheckCodeContainer');");
                    this.Alert("验证码错误或已过期，请重新录入！");
                    return;
                }
                if (!msBsc.SvRef.UserInfo.Online)
                {
                    this.Alert("你未登录，请登录后再抽奖！");
                    return;
                }
                using (msBsc.AppLogic.DataAlc.CBizShpDrawPub opub = new AppLogic.DataAlc.CBizShpDrawPub())
                {
                    string strWinno= opub.doPartakeDrawing(this.GetParamHtValue("ID"));
                    //this.ExecJScript("$('#divshowpartake').remove();");
                    this.ExecJScript("$('#divshowpartake').html('（<span >你的抽奖号码：" + strWinno + "</span>）');");
                }
                 
            } 
        }

    }
}